package fr.afcepf.atod19.voyage.dao.implementation;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import fr.afcepf.atod19.voyage.dao.interfaces.IDataUtilisateur;
import fr.afcepf.atod19.voyage.entities.Client;


@Component
public class DaoUtilisateur implements IDataUtilisateur {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public Client ajouterUtilisateur(Client c) {

		Client cli = entityManager.find(Client.class, c.getIdClient());
		// if (null != c.getReservations())
		// {
		// for (Reservation r : c.getReservations())
		// {
		// if (r.getIdReservation() == 0)
		// {
		// if (null != r.getAdministrateur())
		// {
		// Administrateur a = r.getAdministrateur();
		// if (a.getIdAdministrateur() == 0)
		// {
		// //entityManager.persist(a);
		// }
		// }
		//
		// if (null != r.getVoyage())
		// {
		// Voyage v = r.getVoyage();
		// if (v.getIdVoyage() == 0)
		// {
		// if (null != v.getActivites())
		// {
		// for (Activite act : v.getActivites())
		// {
		// if (act.getIdActivite() == 0)
		// {
		// entityManager.persist(act);
		// }
		// }
		// }
		//
		// if (null != v.getTransports())
		// {
		// for (Transport tran : v.getTransports())
		// {
		// if (tran.getIdTransport() == 0)
		// {
		// entityManager.persist(tran);
		// }
		// }
		// }
		//
		// if (null != v.getHebergement())
		// {
		// Hebergement heb = v.getHebergement();
		// if (heb.getIdHebergement() == 0)
		// {
		// entityManager.persist(heb);
		// }
		// }
		//
		// entityManager.persist(v);
		// }
		// }
		// entityManager.persist(r);
		// }
		// }
		// }
		if (null == cli) {
			entityManager.persist(c);
		}

		return c;
	}

	@Override
	public Client modifierUtilisateur(Client c) {
		entityManager.merge(c);
		return c;
	}

	@Override
	public Client consulterUtilisateur(Integer numClient) {
		Client c = null;
		c = entityManager.find(Client.class, numClient);
		return c;
	}

	@Override
	public List<Client> rechercherUtilisateur(Client c) {

		String req;
		Query q;
		if(c!=null)
		{
			req = "select c from Client as c where ";
			
			boolean multipleCritirea = false;
	
			if (c.getNom() != null) {
				req = req.concat(" c.nom=:nc ");
				multipleCritirea = true;
			}
	
			if (c.getPrenom() != null) {
				if (multipleCritirea) {
					req = req.concat(" AND c.prenom=:pc ");
				} else {
					req = req.concat(" c.prenom=:pc ");
					multipleCritirea = true;
				}
			}
	
			if (c.getCivilite() != null) {
				if (multipleCritirea) {
					req = req.concat(" AND c.civilite=:cc ");
				} else {
					req = req.concat(" c.civilite=:cc ");
					multipleCritirea = true;
				}
			}
	
			if (c.getCoordonneesBancaire() != null) {
				if (multipleCritirea) {
					req = req.concat(" AND c.coordonneesBancaire=:bc ");
				} else {
					req = req.concat(" c.coordonneesBancaire=:bc ");
					multipleCritirea = true;
				}
			}
	
			if (c.getNumeroTel() != null) {
				if (multipleCritirea) {
					req = req.concat(" AND c.numeroTel=:tc ");
				} else {
					req = req.concat(" c.numeroTel=:tc ");
					multipleCritirea = true;
				}
			}
	
			if (c.getAdresseMail() != null) {
				if (multipleCritirea) {
					req = req.concat(" AND c.adresseMail=:ac ");
				} else {
					req = req.concat(" c.adresseMail=:ac ");
					multipleCritirea = true;
				}
			}
			
			if (c.getMdp() != null) {
				if (multipleCritirea) {
					req = req.concat(" AND c.mdp=:mdp ");
				} else {
					req = req.concat(" c.mdp=:mdp ");
					multipleCritirea = true;
				}
			}
	
			q = entityManager.createQuery(req);
	
			if (c.getNom() != null) {
				q.setParameter("nc", c.getNom());
			}
	
			if (c.getPrenom() != null) {
				q.setParameter("pc", c.getPrenom());
			}
	
			if (c.getCivilite() != null) {
				q.setParameter("cc", c.getCivilite());
			}
	
			if (c.getCoordonneesBancaire() != null) {
				q.setParameter("bc", c.getCoordonneesBancaire());
			}
	
			if (c.getNumeroTel() != null) {
				q.setParameter("tc", c.getNumeroTel());
			}
	
			if (c.getAdresseMail() != null) {
				q.setParameter("ac", c.getAdresseMail());
			}
			
			if (c.getMdp() != null) {
				q.setParameter("mdp", c.getMdp());
			}
		}
		else
		{
			req = "from Client";
			q = entityManager.createQuery(req);
		}

		return q.getResultList();
	}

	@Override
	public boolean supprimerUtilisateur(Integer numClient) {
		
		entityManager.remove(entityManager.find(Client.class, numClient));
		
		Client cli = entityManager.find(Client.class, numClient);
		
		System.out.println(cli);
		
		return (cli==null);
	}

}
